package mobidever.mygodutch.controls;

import java.math.BigDecimal;

import mobidever.mygodutch.R;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

public class NumberDialog extends Dialog implements
		android.view.View.OnClickListener
{
	private Context mContext;
	private String Num;

	public interface OnNUmberDialogListener
	{
		public abstract void onNumberFinish(BigDecimal pBigDecimal);
	}

	public NumberDialog(Context pContext)
	{
		super(pContext);
		mContext = pContext;

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.number_dialog);

		findViewById(R.id.btnDot).setOnClickListener(this);
		findViewById(R.id.btnOne).setOnClickListener(this);
		findViewById(R.id.btnTwo).setOnClickListener(this);
		findViewById(R.id.btnThree).setOnClickListener(this);
		findViewById(R.id.btnFour).setOnClickListener(this);
		findViewById(R.id.btnFive).setOnClickListener(this);
		findViewById(R.id.btnSix).setOnClickListener(this);
		findViewById(R.id.btnSeven).setOnClickListener(this);
		findViewById(R.id.btnEight).setOnClickListener(this);
		findViewById(R.id.btnNine).setOnClickListener(this);
		findViewById(R.id.btnZero).setOnClickListener(this);
		findViewById(R.id.btnChange).setOnClickListener(this);
		findViewById(R.id.btnOK).setOnClickListener(this);
	}

	public void onClick(View v)
	{
	
		EditText _EditText = (EditText) findViewById(R.id.etDisplay);
		Num = _EditText.getText().toString();
		switch (v.getId())
		{
		case R.id.btnDot:
			if (Num.length() == 0)
			{
				Num += "0.";
			}else if(Num.indexOf('.') == -1) {
				Num += ".";
			}
			break;
		case R.id.btnOne:
			Num += "1";
			break;
		case R.id.btnTwo:
			Num += "2";
			break;
		case R.id.btnThree:
			Num += "3";
			break;
		case R.id.btnFour:
			Num += "4";
			break;
		case R.id.btnFive:
			Num += "5";
			break;
		case R.id.btnSix:
			Num += "6";
			break;
		case R.id.btnSeven:
			Num += "7";
			break;
		case R.id.btnEight:
			Num += "8";
			break;
		case R.id.btnNine:
			Num += "9";
			break;
		case R.id.btnZero:
			if (Num.length() == 0)
			{
				Num += "0.";
			}
			Num += "0";
			break;
		case R.id.btnChange:
			Log.i("Text", "Text1");
			if (Num.length() == 0)
			{
				
			}else if(Num.equals("0.")) {
				Num = "";
			}else {
				Num = Num.substring(0, Num.length()-1);
			}
			break;
		case R.id.btnOK:

			BigDecimal _BigDecimal;
			if (!Num.equals("0.") & Num.length() != 0)
			{
				_BigDecimal = new BigDecimal(Num);
			} else
			{
				_BigDecimal = new BigDecimal(0);
			}
			((OnNUmberDialogListener) mContext).onNumberFinish(_BigDecimal);
			dismiss();
			break;
		default:
			break;

		}
		Log.i("Text", "Text2");
		_EditText.setText(Num);
	}

}
